package chabernac.android.wordbattle;

import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import chabernac.android.tools.Translator;

public class AbstractWordBattleActivity extends Activity implements OnSharedPreferenceChangeListener {
  private static final int DIALOG_LOAD_LANGUAGE = 1;

  public AbstractWordBattleActivity(){
    init();
  }

  private void init(){
    WordRasterContext.init( this );
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    //    Debug.startMethodTracing("wordbattle");
    getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState);

    WordRasterContext.getInstance().getDatabaseLoaderDecorator().loadLanguage(getSelectedLanguage());

    addPreferenceListener();
  }

  @Override
  protected void onStop() {
    WordRasterContext.getInstance().getStringDatabase().close();
    super.onStop();
  }

  public String getSelectedLanguage(){
    return PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", Locale.getDefault().getLanguage() );
  }

  private void addPreferenceListener(){
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
  }

  // allow database directory to be specified
  @Override
  public SQLiteDatabase openOrCreateDatabase( String aName, int aMode, CursorFactory aFactory ) {
    if(!aName.endsWith( ".db" )) aName = aName + ".db";
    File theDBFile = getDatabasePath( aName );
    if(!theDBFile.getParentFile().exists()) {
      theDBFile.getParentFile().mkdir();
    }
//    if(aMode == MODE_WORLD_READABLE) return SQLiteDatabase.openDatabase(theDBFile.getAbsolutePath(), null,SQLiteDatabase.OPEN_READONLY);
    return SQLiteDatabase.openDatabase(theDBFile.getAbsolutePath(), null,SQLiteDatabase.CREATE_IF_NECESSARY);
  }
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      WordRasterContext.getInstance().getStringDatabase().close();
      //    Debug.stopMethodTracing();
  }

  @Override
  public File getDatabasePath(String aName) {
    return new File(Environment.getExternalStorageDirectory(), "wordbattle/" +  aName);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
  }


  @Override
  public boolean onMenuItemSelected( int aFeatureId, MenuItem aItem ) {
    if(aItem.getItemId() == R.id.menu_settings){
      Intent settingsActivity = new Intent(this, WordBattlePreferencesActivity.class);
      startActivity(settingsActivity);
      //showDialogAndTrack( DIALOG_SETTINGS );
    }
    return super.onMenuItemSelected( aFeatureId, aItem );
  }

  @Override
  public Dialog onCreateDialog(int aDialogId){
    if(aDialogId == DIALOG_LOAD_LANGUAGE){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage(Translator.translate( "loadLanguage" ))
      .setCancelable(false)
      .setPositiveButton(Translator.translate( "yes"), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          //          loadLanguage(getSelectedLanguage());
        }
      })
      .setNegativeButton(Translator.translate("no"), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          dialog.cancel();
        }
      });
      return builder.create();
    }
    return null;
  }

  @Override
  public void onSharedPreferenceChanged( SharedPreferences aSharedPreferences, String aKey ) {
    WordRasterContext.getInstance().getDatabaseLoaderDecorator().loadLanguage(getSelectedLanguage());
  }
}